package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.entity.SceneDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.MonsterVO;
import com.wan37.logic.global.HandlerManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.MonsterServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import com.wan37.protobuf.message.client2server.game.GameBossScene;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.ConcurrentSet;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : luoyong
 * @date : 2020-07-29 16:23
 **/
public class CGamePvpSceneHandler extends MsgHandler {
    private SceneServiceCache sceneServiceCache;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGamePvpSceneHandler(MessageVO messageVO) {
        super(messageVO);
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameBossScene.CGamePvpSceneMsg cGamePvpSceneMsg = (GameBossScene.CGamePvpSceneMsg) messageVO.getMessage();
        String bossSceneId = createBossScene();
        /**
         * 返回场景构建成功消息
         */
        Game.SResponse sResponse = Game.SResponse.newBuilder()
                .setCode(Common.GAME_PVP_SCENE_SUCCESS)
                .setContent(bossSceneId)
                .build();
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                messageVO.getNetId(), messageVO.getUserId(), PtoNum.GAMESRESPONSE, sResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);

        ByteBuf byteBuf1 = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                cGamePvpSceneMsg.getFightId(), PtoNum.GAMESRESPONSE, sResponse);
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf1);
    }

    public String createBossScene() throws Exception {
        GameBossScene.CGamePvpSceneMsg cGamePvpSceneMsg = (GameBossScene.CGamePvpSceneMsg) messageVO.getMessage();
        String bossSceneId = cGamePvpSceneMsg.getBossSceneId();
        String preSceneId = sceneServiceCache.sceneNameToIdMap.get(cGamePvpSceneMsg.getOldSceneId());
        /**
         * 构建一个临时场景，完善数据并且插入场景缓存中
         */
        SceneServiceCache.Scene scene = sceneServiceCache.new Scene();
        SceneDO sceneDO = new SceneDO();
        sceneDO.setSceneId(bossSceneId);
        scene.setSceneDO(sceneDO);
        scene.setCreatTime(System.currentTimeMillis());
        scene.setPreSceneId(preSceneId);
        //0 永久场景  1 临时场景
        scene.setLabel(1);
        scene.setAllRoleVOMap(new ConcurrentHashMap<>());
        scene.setUserIdSet(new ConcurrentSet<>());
        scene.setGoSceneIdSet(new ConcurrentSet<>());
        sceneServiceCache.sceneNameToIdMap.put(bossSceneId, bossSceneId);
        sceneServiceCache.sceneSet.add(bossSceneId);
        scene.getUserIdSet().add(messageVO.getUserId());
        scene.getGoSceneIdSet().add(preSceneId);
        sceneServiceCache.getSceneServiceMap().put(bossSceneId, scene);
        sceneServiceCache.getSceneServiceMap().get(preSceneId).getGoSceneIdSet().add(bossSceneId);
        CGameMoveHandler cGameMoveHandler = (CGameMoveHandler) HandlerManager.getMsgHandler
                (PtoNum.CGAMEMOVE, messageVO.getUserId(), messageVO.getNetId(), null, messageVO.getCtx());
        cGameMoveHandler.move(preSceneId, bossSceneId, messageVO.getUserId(), bossSceneId);
        cGameMoveHandler.move(preSceneId, bossSceneId, cGamePvpSceneMsg.getFightId(), bossSceneId);
        return bossSceneId;
    }
}
